import java.awt.*;
import javax.swing.*;
import java.io.File;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.concurrent.*;
import java.util.*;
import java.io.*;

class ufo{
	JFrame f = new JFrame("星球大战");
	panel p = new panel();
	shell[] sh= new shell[36];
	BlockingQueue<shell> sss = new ArrayBlockingQueue<> (36);
	int ux,uy;
	int fx,fy;
	int[] cx = new int[6];
	int[] cy = new int[6];
	boolean flag = true;
	boolean stop = false;
	
	public void init(){
		initp();
		f.setLocation(300,10);
		p.setPreferredSize(new Dimension(600,600));
		f.add(p);
		f.addKeyListener(new key(this));
		f.setVisible(true);
		f.pack();
		p.repaint();
	}

	private void initp(){
		for(int i=1;i<36;i++){
			sh[i] = new shell(320,550);
			if(i<6)cx[i] = 600;
			if(i<6)cy[i] = 600;
			try
			{
				sss.put(sh[i]);	
			}
			catch(Exception e){

			}
		}
		ux = 500;
		uy = 120;
		fx = 300;
		fy = 550;
	}

	public void move(int x){
		if(fx>10 && fx<530) {
			fx+=x;
			p.repaint();	
		}
		if (fx<=10 && x>0) {
			fx+=x;
			p.repaint();
		}
		if(fx>=530 && x<0){
			fx+=x;
			p.repaint();
		}
		change(fx);

	}

	private void change(int x){
		shell she;
		int i=0;
		while(i++<36){
			she=sss.poll();
			she.init(fx,fy);
			try{
				sss.put(she);
			}
			catch(Exception e){

			}
			
		}
	}

	public void fire(){
		System.out.println("wwwwwwwww");
		try{
			shell she = sss.take();
			while(she.y>-40){
			p.repaint();
			Thread.sleep(1);
			she.y--;
			if(isBit(she.x,she.y)) {
				System.out.println("bit ufo !!!!!!!!!!!!!!!!!!!");
				she.init(fx,fy);
				p.repaint();
				flag = false;
				int i=0;
				while(i++<6) sss.remove();
				Thread.interrupted();
			}
		}
		she.init(fx,fy);
		sss.put(she);
		}
		catch(Exception e){

		}	
	}

	public boolean isBit(int a,int b){
		int x = ux+60;
		int y = uy+90;
		if(a>ux && a<x && b<y && b>uy) return true;
		else return false;
	}

	public void ufoMove(){
		Random r = new Random();
		int x;
		while(flag){
			x = r.nextInt(4);
			switch(x){
				case 0: if(ux<500)ux+=10;
						else ux-=10;
					break;
				case 1:if(ux>20)ux-=10;
						else ux+=10;
					break;
				case 2:if(uy>20) uy-=10;
						else uy+=10;
					break;
				case 3:if(uy<350) uy+=10;
						else uy-=10;
					break;
			}
			try
			{
				Thread.sleep(2);
			}
			catch(Exception e){

			}
			p.repaint();
		}
		System.out.println("MMMMMMMMMMMMMMMMMMMMMMMM");
		sleep(10);
		int i=0;
		while(++i<4){
			cx[i] = ux;
			cy[i] = uy;
			p.repaint();
			sleep(220);
		}
		i=0;
		while(++i<4) cy[i]=cx[i] = 600;
		p.repaint();
		while(--i>0){
			cx[i] = ux;
			cy[i] = uy;
			p.repaint();
			sleep(220);
		}
		p.repaint();
		while(++i<4) cy[i]=cx[i] = 600;
		ux=uy=600;
		p.repaint();
		JOptionPane.showMessageDialog(null,"游戏结束！你击落了入侵者");
	}

	private void sleep(int s){
		try{
			Thread.sleep(s);
		}
		catch(Exception e){

		}
	}

	public static void main(String[] s){
		ufo u = new ufo();
		u.init();
		new uuu(u).start();
	}

class panel extends JPanel{
	BufferedImage u,b,p,f,c1,c2,c3;
	public panel(){
		try
		{
			u = ImageIO.read(new File("img/u.png"));
			b = ImageIO.read(new File("img/b.png"));
			p = ImageIO.read(new File("img/p.png"));
			f = ImageIO.read(new File("img/f.png"));
			c1 = ImageIO.read(new File("img/1.jpg"));
			c2 = ImageIO.read(new File("img/2.jpg"));
			c3 = ImageIO.read(new File("img/3.jpg"));
		}
		catch(Exception e){
		}
		
	}
	public void paintComponent(Graphics g){

		//super.paintComponent(g);
		g.drawImage(b,0,0,600,600,this);
		g.drawImage(u,ux,uy,85,55,this);

		int i=0;
		while(++i<36) g.drawImage(p,sh[i].x,sh[i].y,20,35,this);
				
		g.drawImage(f,fx,fy,65,40,this);

		g.drawImage(c1,cx[1],cy[1],90,60,this);
		g.drawImage(c2,cx[2],cy[2],90,60,this);
		g.drawImage(c3,cx[3],cy[3],90,60,this);
	}
	
}

class key extends KeyAdapter{
		ufo u;
		public key(ufo x){
			this.u = x;
		}
        public void keyPressed(KeyEvent e){  
            if(e.getKeyCode()==KeyEvent.VK_UP) {
            	System.out.println("   up  ;");
            	new thread(u).start();
            }
            if(e.getKeyCode()==KeyEvent.VK_LEFT){
            	System.out.println("   left  ;");
            	new fff(u,-30).start();
            }
            if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            	System.out.println("   right  ;");
            	new fff(u,30).start();
            }
        }  
    }  

}


class shell{
	public int x,y;
	private int a,b;
	public shell(int a,int b){
		this.x = a;
		this.y = b;
		this.a = a;
		this.b = b;
	}
	public void init(int x1,int y1){
		this.x = x1+20;
		this.y = b;
	}
	private void setX(int x){
		this.x = x;
	}
	private void setY(int y){
		this.y = y;
	}

}

class thread extends Thread{
	private ufo u;
	public thread(ufo x){
		this.u = x;
	}
	public void run(){
		u.fire();
	}
}

class fff extends Thread{
	private ufo u;
	private int x;
	public fff(ufo x,int y){
		this.u = x;
		this.x = y;
	}
	public void run(){
		u.move(x);
	}
}

class uuu extends Thread{
	private ufo u;
	public uuu(ufo x){
		this.u = x;
	}
	public void run(){
		u.ufoMove();
	}
}
